import 'package:flutter/material.dart';
import 'package:flutter_demo/demo/async/gesture_page.dart';
import 'package:flutter_demo/demo/image/photo_app_page.dart';
import 'package:flutter_demo/demo/image/res_page.dart';
import 'package:flutter_demo/demo/layout/layout_page.dart';
import 'package:flutter_demo/demo/route/launch_page.dart';
import 'package:flutter_demo/demo/widget/less_group_page.dart';
import 'package:flutter_demo/demo/widget/stateful_group_page.dart';

void main() => runApp(ImoocDemo());

class ImoocDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'imooc demo navigator page',
      theme: ThemeData.fallback(),
      home: Scaffold(
        appBar: AppBar(
          title: Text('路由和导航'),
        ),
        body: RouteNavigator(),
      ),
      routes: <String, WidgetBuilder>{
        'less': (context) => LessGroupPage(),
        'ful': (context) => StatefulGroupPage(),
        'layout': (context) => LayoutPage(),
        'gesture': (context) => GesturePage(),
        'image': (context) => ResPage(),
        'launch': (context) => LaunchPage(),
        'photo': (context) => PhotoAppPage(),
      },
    );
  }
}

class RouteNavigator extends StatefulWidget {
  @override
  _RouteNavigatorState createState() => _RouteNavigatorState();
}

class _RouteNavigatorState extends State<RouteNavigator> {
  bool byName = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          SwitchListTile(
            title: Text('${byName ? '' : '不'}通过路由名跳转'),
            value: byName,
            onChanged: (value) {
              setState(() {
                byName = value;
              });
            },
          ),
          _item('StatelessWidget 与基础组件', LessGroupPage(), 'less'),
          _item('StatefulWidget 与基础组件', StatefulGroupPage(), 'ful'),
          _item('Layout Widgets Demo', LayoutPage(), 'layout'),
          _item('检测用户手势', GesturePage(), 'gesture'),
          _item('导入和使用资源文件', ResPage(), 'image'),
          _item('打开第三方应用', LaunchPage(), 'launch'),
          _item('拍照APP', PhotoAppPage(), 'photo'),
        ],
      ),
    );
  }

  _item(String title, page, String routeName) {
    return RaisedButton(
      child: Text(title),
      onPressed: () {
        if (byName) {
          Navigator.pushNamed(context, routeName);
        } else {
          Navigator.push(context, MaterialPageRoute(builder: (context) => page));
        }
      },
    );
  }
}
